package cn.bdqn.veh.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.mapper.VehicleMapper;
import cn.bdqn.veh.mapper.VldMapper;
import cn.bdqn.veh.model.*;
import cn.bdqn.veh.service.VehicleService;
import cn.bdqn.veh.vomapper.VehicleVoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-20 17:51
 */
@Service
public class VehicleServiceImpl extends BaseService implements VehicleService {

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private VehicleVoMapper vehicleVoMapper;

    @Resource
    private VldMapper vldMapper;

    @Override
    public EUList<Vld> list(PageBean pageBean, String cidcard, String phone, String idcard, String cname, String lname) {
        VldExample example = new VldExample();
        VldExample.Criteria criteria = example.createCriteria();
        criteria.andIsdelEqualTo(false);
        criteria.andEidEqualTo(getEid());   //从session来
        criteria.andIsstopEqualTo(false);
        if(!StringUtils.isEmpty(cidcard) || !StringUtils.isEmpty(phone) || !StringUtils.isEmpty(cname) || !StringUtils.isEmpty(lname) || !StringUtils.isEmpty(idcard)){
//            criteria.andAccountEqualTo(username);
            criteria.andCidcardLike("%"+cidcard+"%");
            criteria.andIdcardLike("%"+idcard+"%");
            criteria.andLnameLike("%"+lname+"%");
            criteria.andCnameLike("%"+cname+"%");
            criteria.andPhoneLike("%"+phone+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Vld> vlds = vldMapper.selectByExample(example);

        PageInfo<Vld> info=new PageInfo<>(vlds);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Vehicle vehicle) {
        if(vehicle.getId()==null){
            vehicle.setEid(getEid());

            vehicleMapper.insertSelective(vehicle);
        }else{
            vehicleMapper.updateByPrimaryKeySelective(vehicle);    //更新时不更新帐号
        }
        return R.ok();
    }

    @Override
    public R delete(Integer id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setIsdel(true);
//        try{
        vehicleMapper.updateByPrimaryKeySelective(vehicle);
        return R.ok();
    }

    @Override
    public R getAllTruckType() {
        List<TruckType> truckTypes = vehicleVoMapper.getAllTruckType();
        return R.ok(null, truckTypes);
    }

    @Override
    public R getAllLeader() {
        List<Leader> leaders = vehicleVoMapper.getAllLeader();
        return R.ok(null, leaders);
    }

    @Override
    public R getAllColor() {

        List<LicensePlateColor> licensePlateColors = vehicleVoMapper.getAllColor();
        return R.ok(null, licensePlateColors);
    }

    @Override
    public R auth(Integer id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setIsauth(true);
        Integer userId = getUserid();
        vehicle.setOpid(userId);
        vehicleMapper.updateByPrimaryKeySelective(vehicle);
        return R.ok();
    }
}
